<?php  if (!defined('BASEPATH') || !defined('CRON')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/main.php';

/**
 * Контроллер создания карты сайта
 *
 * @package CoreFramework
 * @version 1.0
 * @author Obidnov Alexander
 */
class Cron_Screen extends Main {

   /**
    * Конструктор класса
    *
    * @access public
    * @return Cron_Screen
    */
   function Cron_Screen() {
      parent::Main();
   } // end Cron_Screen

   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {

      set_time_limit(0);

      $this->load->model('m_table');
      $this->load->model('m_video');
      $this->load->helper('content');
      $this->load->library('image_lib');

      // Получаем страницы с видео
      $this->m_table->set_table('videos');
      $this->m_table->add_restriction('is_watched', 'no');
      $this->m_table->add_restriction('num_torrents', 0, '>');
      $this->m_table->add_sort('date_created', false);
      $this->m_table->load_results();
      $contents = $this->m_table->get_results();

      $sizes = array(array('width' => 300, 'height' => 100), array('width' => 200, 'height' => 50));

      foreach ($contents as $key=>$row) {
         $this->m_video->set_id($row['id_video']);
         $screenshots = $this->m_video->get_screenshots(false);
         $this->m_video->set('is_watched', 'yes');
         $this->m_video->update();

         foreach ($screenshots as $screenshot) {

            foreach ($sizes as $size) {
               $width = $size['width'];
               $height = $size['height'];
               $thumbnail = preg_replace('~^(.*)\.([^\.]+)$~msi', '$1-' . $width . 'x' . $height . '.$2', $screenshot['image']);
               if (!file_exists(BASEPATH . '../' . $thumbnail)) {
                  // Если такого файла еще не сушествует, создаем его
                  $master_dim = 'auto';
                  $scale = true;
                  // Получаем исходные размеры изображения
                  $info = @getimagesize(BASEPATH . '../' . $screenshot['image']);
                  
                  if (is_array($info) && 0 < count($info)) {
                     $cur_width = $info[0];
                     $cur_height = $info[1];
                     if ($cur_width < $width && $cur_height < $height) {
                        $scale = false;
                     }
                     $master_dim = ($cur_width / $width) > ($cur_height / $height) ? 'width' : 'height';
                  }
                  if ($scale) {
                     $config = array(
                             'source_image'   => BASEPATH . '../' . $screenshot['image'],
                             'new_image'      => BASEPATH . '../' . $thumbnail,
                             'maintain_ratio' => true,
                             'width'          => $width,
                             'height'         => $height,
                             'master_dim'     => $master_dim
                     );
                     $this->image_lib->initialize($config);
                     $this->image_lib->resize();
                  } else {
                     copy(BASEPATH . '../' . $screenshot['image'], BASEPATH . '../' . $thumbnail);
                     chmod(BASEPATH . '../' . $thumbnail, 0777);
                  }

               }
               
            }
            @unlink(BASEPATH . '../' . $screenshot['image']);
         }
         //if ($key>1)exit;
      }

      echo 'done';
   } // end index

}
